// 日记数据模型
interface DiaryItem {
  id: string;
  content: string;
  mood: string;
  date: string;
  timestamp: number;
}

// 周记数据模型
interface WeeklyStory {
  id: string;
  content: string;
  startDate: string;
  endDate: string;
  diaryIds: string[];
  timestamp: number;
}

// 获取本地存储的日记列表
const getDiaryList = (): DiaryItem[] => {
  try {
    const diaryList = wx.getStorageSync('diaryList') || []
    return diaryList
  } catch (e) {
    console.error('获取日记列表失败', e)
    return []
  }
}

// 保存日记到本地存储
const saveDiary = (diary: DiaryItem): boolean => {
  try {
    const diaryList = getDiaryList()
    diaryList.unshift(diary)
    wx.setStorageSync('diaryList', diaryList)
    return true
  } catch (e) {
    console.error('保存日记失败', e)
    return false
  }
}

// 获取本地存储的周记列表
const getWeeklyList = (): WeeklyStory[] => {
  try {
    const weeklyList = wx.getStorageSync('weeklyList') || []
    return weeklyList
  } catch (e) {
    console.error('获取周记列表失败', e)
    return []
  }
}

// 保存周记到本地存储
const saveWeekly = (weekly: WeeklyStory): boolean => {
  try {
    const weeklyList = getWeeklyList()
    weeklyList.unshift(weekly)
    wx.setStorageSync('weeklyList', weeklyList)
    return true
  } catch (e) {
    console.error('保存周记失败', e)
    return false
  }
}

// 获取最近一周的日记
const getLastWeekDiaries = (): DiaryItem[] => {
  const diaryList = getDiaryList()
  const now = Date.now()
  const oneWeekAgo = now - 7 * 24 * 60 * 60 * 1000
  
  return diaryList.filter(diary => diary.timestamp >= oneWeekAgo)
}

export {
  DiaryItem,
  WeeklyStory,
  getDiaryList,
  saveDiary,
  getWeeklyList,
  saveWeekly,
  getLastWeekDiaries
} 